/*
 *  Class name: LoginBean.java
 *  Version: 1.0
 *  Date: 4.1.2014
 */
package cz.muni.fi.pa165.soccergamesarchive.managedbeans.application;

import java.io.Serializable;
import java.util.ResourceBundle;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import static javax.faces.context.FacesContext.getCurrentInstance;

/**
 *
 * @author Michal Ďuriš
 */
@Named(value = "loginBean")
@SessionScoped
public class LoginBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    DatabaseDataCreatorBean databaseDataCreator;

    @Inject
    NavigationMenuBean navigationMenuBean;

    private String username;
    private String password;

    private boolean logedIn = false;

    /**
     * Creates a new instance of LoginBean
     */
    public LoginBean() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setLogedIn(boolean logedIn) {
        this.logedIn = logedIn;
    }

    public boolean isLogedIn() {
        return logedIn;
    }

    public void login() {

        HttpServletRequest httpServletRequest
                = (HttpServletRequest) getCurrentInstance().getExternalContext()
                .getRequest();
        try {
            httpServletRequest.login(username, password);

            logedIn = true;
        } catch (ServletException ex) {

            //TODO: Logger
            System.err.println("Exception in login: " + ex);

            getCurrentInstance().addMessage("loginDialogWindow",
                    new FacesMessage(
                            FacesMessage.SEVERITY_ERROR, getResourceBundle()
                            .getString(
                                    "loginFailure"), getResourceBundle()
                            .getString(
                                    "loginFailure")));
        }

        if (logedIn) {
            try {
                databaseDataCreator.storeData();
            } catch (RuntimeException ex) {
                System.err.println("Testing data creation failed... " + ex);
            }
        }
    }

    public String logout() {
        HttpServletRequest httpServletRequest
                = (HttpServletRequest) getCurrentInstance().getExternalContext()
                .getRequest();
        try {
            httpServletRequest.logout();

            logedIn = false;
        } catch (ServletException ex) {

            //TODO: Logger
            System.err.println("Logout fails: " + ex);

            getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, getResourceBundle().getString(
                            "logoutFailure"), getResourceBundle().getString(
                            "logoutFailure")));
        }

        return navigationMenuBean.getHomeLocation();
    }

    private ResourceBundle getResourceBundle() {
        return getCurrentInstance().getApplication().getResourceBundle(
                getCurrentInstance(), "bundle");
    }

}
